package  
{
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.utils.ByteArray;
	import starling.core.RenderSupport;
	import starling.core.Starling;
	
	public class ImageSaver 
	{
		private var mBmpList:Vector.<ByteArray> = new Vector.<ByteArray>();
		
		public function ImageSaver() 
		{
			
		}
		
		public function CaptureScreen():void
		{
			var bmpData:BitmapData = new BitmapData(1280, 720, false, 0x000000);
			var support:RenderSupport = new RenderSupport();
			Starling.context.clear();
			support.setOrthographicProjection(0, 0, 1280, 720);
			Starling.current.root.render( support, 1.0 );
			support.finishQuadBatch();
			Starling.context.drawToBitmapData(bmpData);
			mBmpList.push(PNGEncoder.encode(bmpData));
		}
		
		public var mDumping:Boolean = false;
		private var mIndex:int = 0;
		public function Dump():void
		{
			if (!mDumping && mIndex < mBmpList.length)
			{
				mDumping = true;
				
				var mFS:FileStream = new FileStream();
				mFS.addEventListener(Event.CLOSE, OnClose);
				var targetFile:File = File.desktopDirectory.resolvePath("boidImage/test"+mIndex.toString()+".png");
				
				mFS.openAsync(targetFile, FileMode.WRITE);
				mFS.writeBytes(mBmpList[mIndex]);
				mFS.close();
				
				trace("Saving " + mIndex.toString() +".png to disk...");
				mIndex++;
			}
		}
		
		private function OnClose(aEvent:Event):void
		{
			mDumping = false;
			trace("Saving complete!");
		}
	}
}